/**
 * @Title: UserServiceImpl.java
 * @Package me.springboot.mybatis.service.impl
 * @Description: TODO
 * @author zengh
 * @date 2018/04/23
 * @version V1.0
 */
package me.springboot.mybatis.service.impl;

import me.springboot.mybatis.common.utils.SessionUtil;
import me.springboot.mybatis.core.AbstractService;
import me.springboot.mybatis.core.RetCode;
import me.springboot.mybatis.dao.UserMapper;
import me.springboot.mybatis.exception.ResultException;
import me.springboot.mybatis.model.User;
import me.springboot.mybatis.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zengh
 * @ClassName: UserService
 * @Description: TODO
 * @date 2018/04/23
 */
@Service
@Transactional
public class UserServiceImpl extends AbstractService<User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public Map<String, Object> login(HttpServletRequest request, String account, String password) throws ResultException {
        User loginUser = new User();
        loginUser.setAccount(account);
        loginUser.setPassword(password);
        User user = userMapper.selectByModel(loginUser);

        if (null != user) {
            if(user.getType().equals("2")) {
                throw new ResultException(RetCode.BUS_FALL.getCode(), "账户或密码错误");
            }
            String token = SessionUtil.setSessionUser(request, user);
            Map<String, Object> resultMap = new HashMap();
            resultMap.put("token", token);
            resultMap.put("user", user);
            return resultMap;
        } else {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "账户或密码错误");
        }
    }

    @Override
    public Map<String, Object> chargeMyPwd(HttpServletRequest request, String oldPwd, String newPwd) throws ResultException {
        User user = SessionUtil.getSessionUser(request);
        if (!oldPwd.equals(user.getPassword())) {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "旧密码错误");
        }

        Map<String, Object> param = new HashMap<>();
        param.put("uid", user.getuId());
        param.put("password", newPwd);

        userMapper.chargePwd(param);
        SessionUtil.removeUser(request);
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("msg", "修改成功");
        return resultMap;
    }

    @Override
    public Map<String, Object> chargePwd(HttpServletRequest request, Integer uid, String oldPwd, String newPwd) throws ResultException {
        User user = userMapper.selectByPrimaryKey(uid);
        if (null != user) {
            if (!oldPwd.equals(user.getPassword())) {
                throw new ResultException(RetCode.BUS_FALL.getCode(), "旧密码错误");
            }
            user.setPassword(newPwd);
            userMapper.updateByPrimaryKey(user);
        } else {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "用户不存在");
        }
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("msg", "修改成功");
        return resultMap;
    }

    @Override
    public Map<String, Object> resertPwd(String uid) {
        Map<String, Object> param = new HashMap<>();
        param.put("uid", uid);
        userMapper.resertPwd(param);
        Map<String, Object> resultMap = new HashMap();
        resultMap.put("msg", "重置成功");
        return resultMap;
    }

    @Override
    public List<User> selectByMap(Map<String, Object> param) {
        return userMapper.selectByMap(param);
    }

    @Override
    public Map<String, Object> phoneLogin(HttpServletRequest request, String account, String password) throws ResultException {
        User loginUser = new User();
        loginUser.setAccount(account);
        loginUser.setPassword(password);
        loginUser.setType("2");
        User user = userMapper.selectByModel(loginUser);

        if (null != user) {
            String token = SessionUtil.setSessionUser(request, user);
            Map<String, Object> resultMap = new HashMap();
            resultMap.put("token", token);
            resultMap.put("user", user);
            return resultMap;
        } else {
            throw new ResultException(RetCode.BUS_FALL.getCode(), "账户或密码错误");
        }
    }
}